package com.mf.ergate.web.controller;

import com.mf.ergate.web.domain.model.MenuTree;
import com.mf.ergate.web.security.LoginContext;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * 默认控制器
 * User: zhaoming
 * DateTime: 2017-11-06
 * To change this template use File | Settings | File Templates.
 */
@Controller
public class DefaultController {

    public Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 首页
     */
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String index() {
        return "index";
    }

    /**
     * 登录页
     */
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login(String error, Model model) {
        if (StringUtils.isNotEmpty(error)) {
            model.addAttribute("error", error);
        }
        return "login";
    }

    /**
     * 菜单
     */
    @RequestMapping(value = "/menu", method = RequestMethod.POST)
    @ResponseBody
    public List<MenuTree> menu() {
        List<MenuTree> menuTrees = new ArrayList<>();
        if (LoginContext.getCurrentUser().getMenuTree() != null) {
            menuTrees = LoginContext.getCurrentUser().getMenuTree().getMenus();
        }
        return menuTrees;
    }
}
